package tester;

import java.util.Arrays;

public class MultipleQuestion extends Question{
	private int[] correctAnswers;

	MultipleQuestion(String questionText, String[] answers, 
			String correctAnswers) {
		super(questionText, "Choose all correct answers:", 
				"[\\s*\\d{1}\\s*]+", answers);
		String[] correctAnswersArray = correctAnswers.split("\\s");
		this.correctAnswers = new int[correctAnswersArray.length];
		for (int i = 0; i < this.correctAnswers.length; i++) {
			this.correctAnswers[i] = new Integer(correctAnswersArray[i]);
		}

	}
	
	@Override
	protected boolean checkAnswer(String answer) {
		if (!validateAnswer(this.regexp, answer)) {
			return false;
		}
		return (Arrays.equals(this.correctAnswers, convertAnswer(answer)));
	}
	
	protected int[] convertAnswer(String answer) {
		answer  = answer.replaceAll("\\s{2,}", " ").trim();
		String[] answersArray = answer.split("\\s");
		int[] result = new int[answersArray.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = new Integer(answersArray[i]);
		}
		return result;
	}
}
